package com.example.pda.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.example.pda.R;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.SharedUtils;
import com.example.pda.common.Utils;
import com.example.pda.databinding.ActivityLoginBinding;
import com.example.pda.net.HttpManager;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class SettingActivity extends BaseActivity {

    private LoginViewModel loginViewModel;
    private ActivityLoginBinding binding;

    private String mBaseUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        binding.setLifecycleOwner(this);
        loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory())
                .get(LoginViewModel.class);
        registerObserve(loginViewModel);

        binding.username.setText(SharedUtils.getIP());
        binding.password.setText(SharedUtils.getPort());

//        loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() {
//            @Override
//            public void onChanged(@Nullable LoginFormState loginFormState) {
//                if (loginFormState == null) {
//                    return;
//                }
//                binding.login.setEnabled(loginFormState.isDataValid());
//                if (loginFormState.getUsernameError() != null) {
//                    binding.username.setError(getString(loginFormState.getUsernameError()));
//                }
//                if (loginFormState.getPasswordError() != null) {
//                    binding.password.setError(getString(loginFormState.getPasswordError()));
//                }
//            }
//        });

        loginViewModel.getLoginResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean loginResult) {
                if (loginResult != null && loginResult) {
                    updateUiWithUser();
                    String http = binding.username.getText().toString().trim();
                    String httpPort = binding.password.getText().toString().trim();
                    if (!TextUtils.isEmpty(http) && !TextUtils.isEmpty(httpPort)) {
                        SharedUtils.setIP(binding.username.getText().toString().trim());
                        SharedUtils.setPort(binding.password.getText().toString().trim());
                        HttpManager.getInstance().init(mBaseUrl);
                        finish();
                    } else {
                        Utils.getInstance().showToast("请输入IP地址或端口号");
                    }
                }
            }
        });

//        TextWatcher afterTextChangedListener = new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                // ignore
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                // ignore
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                loginViewModel.loginDataChanged(binding.username.getText().toString(),
//                        binding.password.getText().toString());
//            }
//        };
//        binding.username.addTextChangedListener(afterTextChangedListener);
//        binding.password.addTextChangedListener(afterTextChangedListener);

        binding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String http = binding.username.getText().toString().trim();
                String httpPort = binding.password.getText().toString().trim();
                if (!TextUtils.isEmpty(http) && http.startsWith("http://")) {
                    if(!TextUtils.isEmpty(httpPort)){
                        mBaseUrl = http + ":" + httpPort + "/";
                    } else {
                        mBaseUrl = http + "/";
                    }
                    loginViewModel.loginCheck(mBaseUrl);
                } else if (!TextUtils.isEmpty(http) && !TextUtils.isEmpty(httpPort)) {
                    mBaseUrl = "http://" + http + ":" + httpPort + "/";
                    loginViewModel.loginCheck(mBaseUrl);
                } else {
                    Utils.getInstance().showToast("请输入IP地址或端口号");
                }
            }
        });
    }

    private void updateUiWithUser() {
        String welcome = getString(R.string.settings_success);
        Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show();
    }
}